package com.suyuan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suyuan.dao.CompanyInfoMapper;
import com.suyuan.dao.FeedsMapper;
import com.suyuan.dao.VaccineInforMapper;
import com.suyuan.entity.Feeds;
import com.suyuan.entity.FeedsExample;
import com.suyuan.entity.VaccineInfor;
import com.suyuan.entity.VaccineInforExample;
import com.suyuan.service.FeedsService;
import com.suyuan.service.VaccineInfoService;
import com.suyuan.web.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service(value = "feedsService")
public class FeedsServiceImpl implements FeedsService {

    @Resource
    private FeedsMapper feedsMapper;

    public int save(Feeds feeds){
        feeds.setAmountStore(0.0f);
        return feedsMapper.insert(feeds);
    }

    public int delete(Integer feedid) {
        return feedsMapper.deleteByPrimaryKey(feedid);
    }

    /**
     * 根据饲料名称、饲料种类、供应商查询饲料信息
     * @param feedname 饲料名称
     * @param component 饲料种类
     * @param supplierid 供应商
     * @param page
     * @return
     */
    public PageInfo inquiry(String feedname,String component,Integer supplierid, Page page){
        FeedsExample feedsExample=new FeedsExample();
        FeedsExample.Criteria criteria=feedsExample.createCriteria();
        if(feedname!=null){
            criteria.andFeednameLike("%"+feedname+"%");
        }
        if(component!=null){
            criteria.andComponentEqualTo(component);
        }
        if(supplierid!=null){
            criteria.andSupplieridEqualTo(supplierid);
        }
        PageHelper.startPage(page.getPageNumber(),page.getPageSize());
        List<Feeds> feeds=feedsMapper.selectByExample(feedsExample);
        PageInfo info = new PageInfo(feeds);
        return info;
    }

    public Feeds findByfeedId(Integer feedsid){
        FeedsExample feedsExample=new FeedsExample();
        FeedsExample.Criteria criteria=feedsExample.createCriteria();
        if(feedsid!=null){
            criteria.andFeedidEqualTo(feedsid);
            return feedsMapper.selectByPrimaryKey(feedsid);
        }
        return null;
    }

    public int update(Feeds feeds){
        Feeds feeds1=feedsMapper.selectByPrimaryKey(feeds.getFeedid());
        feeds1.setAmountStore(feeds.getAmountStore());
        int code=feedsMapper.updateByPrimaryKeySelective(feeds1);
        return code;
    }

    public List<Feeds> findAll(){
        FeedsExample feedsExample=new FeedsExample();
        FeedsExample.Criteria criteria=feedsExample.createCriteria();
        List<Feeds> feeds=feedsMapper.selectByExample(feedsExample);
        return feeds;
    }
}
